package org.saratogaschools.edd.firefighter.client;

import org.saratogaschools.edd.firefighter.client.place.EnterNamePlace;
import org.saratogaschools.edd.firefighter.client.place.FireListPlace;
import org.saratogaschools.edd.firefighter.client.util.AsyncPlaceChange;
import org.saratogaschools.edd.firefighter.shared.model.FireFighter;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AegisFirefighter implements EntryPoint {
	
	/**
	 * This is the entry point method.
	 */
	
	private ClientFactory clientFactory;
	
	public AegisFirefighter() {
		this.clientFactory = GWT.create(ClientFactory.class);
	}
	
	
	public void onModuleLoad() {
		clientFactory.getResources().style().ensureInjected();
		RootLayoutPanel.get().add(clientFactory.getDisplay());
		clientFactory.getService().getFireFighter(new AsyncCallback<FireFighter>() {
			public void onSuccess(FireFighter result) {
				if (result == null) {
					// Get registered
					GWT.runAsync(new AsyncPlaceChange(clientFactory, new EnterNamePlace()));
					
				}
				else {
					GWT.runAsync(new AsyncPlaceChange(clientFactory, new FireListPlace(result)));
				}
			}
			public void onFailure(Throwable caught) {
				// TODO implement
			}
		});
		
	}
}
